package com.yjhb.android.sbz.mvp.rechargehistory;

import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.yjhb.android.sbz.entity.Order;
import com.yjhb.android.sbz.http.HttpObserver;
import com.yjhb.android.sbz.http.HttpTool;
import com.yjhb.android.sbz.mvp.ConventionPresenter;

import java.util.List;

/**
 * 创建者 zx
 * 创建时间 2020/12/3 11:24
 */
public class RechargeHistoryPresenter extends ConventionPresenter<RechargeHistoryView> {
    private final String TAG = "RechargePresenter";


    public RechargeHistoryPresenter(RechargeHistoryView rechargeView) {
        super(rechargeView);
    }

    @Override
    protected void onDestroy() {

    }


    public void getHistory(int size, final int current) {
        HttpTool.request(model.selectUserOrderIn(size, current), new HttpObserver<JsonObject>() {
            @Override
            public void onResult(JsonObject data) {
                if (view != null) {
                    List<Order> rechargeHistories = new GsonBuilder().
                            setDateFormat("yyyy-MM-dd HH:mm:ss").create().fromJson(data.get("records"),
                            new TypeToken<List<Order>>(){}.getType());
                    if (current > 1) {
                        view.onLoadHistory(rechargeHistories);
                    } else {
                        view.onSuccess(rechargeHistories);
                    }
                }
            }

            @Override
            public void onError(int code, String msg) {
                if (view != null) {
                    view.onFailure(code, msg);
                }

            }
        });
    }

}
